import {Component, OnInit} from '@angular/core';
import {AppContextService} from '../../shared/services/app-context.service';
import {NavigationExtras} from '@angular/router';

@Component({
  selector : 'app-login',
  templateUrl : './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
  userName: string;
  userPwd: string;

  constructor(
    private app: AppContextService
  ) {}

  ngOnInit() {
  }


  login() {
    this.app.login(this.userName, this.userPwd)
      .subscribe(result => {
        if (result.success) {
          const redirect = this.app.redirectUrl
            ? this.app.redirectUrl
            : '';
          const navgationExtras: NavigationExtras = {
            preserveQueryParams: true,
            preserveFragment: true
          };
          this.app.router.navigate([redirect], navgationExtras);
        }
        this.app.msg.info(result.message);
      });
  }

  register() {
    this.app.router.navigate(['/']);
  }

}
